<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="城市：">
          <city-select v-model="formInline.cities"></city-select>
        </el-form-item>
        <el-form-item label="报名时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约时间：">
          <el-date-picker v-model="formInline.rangTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户需求：">
          <project-select v-model="formInline.projects"></project-select>
        </el-form-item>
        <el-form-item label="客户来源：">
          <user-source v-model="formInline.clueSources"></user-source>
        </el-form-item>
        <el-form-item label="广告代号：">
          <adcode-select v-model="formInline.adSources"></adcode-select>
        </el-form-item>
        <el-form-item label="负责人：">
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <!-- <el-button type="primary" size="small">导出excel</el-button> -->
        <auth-button type="default" uid="1010501" size="small" :loading="eloading" @click.native="downloadExcel">导出excel</auth-button>
      </div>
      <!-- 操作区域 end -->
      <el-table ref="elTable" class="el_table" size="small" show-summary :summary-method="getSummaries" v-loading="loading" :data="tableData">
        <el-table-column prop="cityName" label="城市" show-overflow-tooltip />
        <el-table-column prop="hospitalCnt" label="上线医院数" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.hospitalCnt||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="newCustomerCnt" label="线索数" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.newCustomerCnt||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="newAppointmentCnt" label="总预约数" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.newAppointmentCnt||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="appointmentCnt" label="到期预约数" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.appointmentCnt||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="arrivedCnt" label="到店数" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.arrivedCnt||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="arrivedDealCnt" label="成交数" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.arrivedDealCnt||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="dealAmount" label="成交金额" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.dealAmount||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="collectMoney" label="收款金额" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.collectMoney||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="appointmentArrivedRate" label="线索预约率" show-overflow-tooltip>
          <template slot-scope="{row}">{{((row.appointmentArrivedRate||0)*100).toFixed(0)}}%</template>
        </el-table-column>
        <el-table-column prop="arrivedDealRate" label="预约到店率" show-overflow-tooltip>
          <template slot-scope="{row}">{{((row.arrivedDealRate||0)*100).toFixed(0)}}%</template>
        </el-table-column>
        <el-table-column prop="averageClueUnitPrice" label="到店成交数" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.averageClueUnitPrice||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="averageClueUnitPrice" label="线索产值" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.averageClueUnitPrice||0).toFixed(0)}}</template>
        </el-table-column>
        <el-table-column prop="averageArrivedUnitPrice" label="到店产值" show-overflow-tooltip>
          <template slot-scope="{row}">{{(row.averageArrivedUnitPrice||0).toFixed(0)}}</template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

  </div>
</template>
  
  <script>
import { GETCITYOVERVIEWSEARCH, GETCITYOVERVIEWEXPORT } from "@/http/system";
import { showToast, showModal, deepClone } from "@/common/util";
import { exportExcel } from "@/common/common";
import moment from "moment";
export default {
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      eloading: false,
      summary: {},
    };
  },
  created() {
    let end = moment().format("YYYY-MM-DD");
    let start = moment()
      .day(moment().day() - 29)
      .format("YYYY-MM-DD");
    this.$set(this.formInline, "rangTime", [start, end]);
    this.$set(this.formInline, "orderTime", [start, end]);
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 重置 */
    rest() {
      let end = moment().format("YYYY-MM-DD");
      let start = moment()
        .day(moment().day() - 29)
        .format("YYYY-MM-DD");
      this.formInline = { rangTime: [start, end], orderTime: [start, end] };
      this.getList();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let oParams = deepClone(this.formInline);
      let n = oParams.orderTime || [];
      let m = oParams.rangTime || [];

      if (n && n.length > 0) {
        oParams.enrollStartTime = n[0];
        oParams.enrollEndTime = n[1];
      }

      if (m && m.length > 0) {
        oParams.beginDate = m[0];
        oParams.endDate = m[1];
      }

      delete oParams.orderTime;
      delete oParams.rangTime;

      oParams.pageNumber = this.pageNumber;
      oParams.pageSize = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      GETCITYOVERVIEWSEARCH(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
        _this.summary = res.summary || {};
      });
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有城市概览列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile();
          },
        });
        return false;
      }

      _this.dowloadExcelFile();
    },

    // 下载excel
    dowloadExcelFile() {
      let _this = this;
      let oParams = _this.getParams();
      let options = {};

      delete oParams.pageNumber;
      delete oParams.pageSize;

      options.method = "GET";
      options.data = oParams;
      options.url = GETCITYOVERVIEWEXPORT;
      options.fileName = "城市概览列表.xlsx";
      _this.eloading = true;
      exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },

    // 自定义展示合计
    getSummaries(param) {
      let { columns, data } = param;
      let summary = this.summary;
      let sums = [];
      let arr = ["appointmentArrivedRate", "arrivedDealRate"];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (arr.indexOf(column.property) != "-1") {
          sums[index] =
            (summary[column.property]
              ? (summary[column.property] * 100).toFixed(0)
              : 0) + "%";
          return;
        }

        sums[index] = summary[column.property]
          ? summary[column.property].toFixed(0)
          : 0;
      });
      return sums;
    },
  },
};
</script>
  
<style lang="scss" scoped>
.a_input,
.a_cascader {
  width: 240px;
}
</style>
  